//
//  JXRetweetView.swift
//  swift高仿微博
//
//  Created by peijiaxing on 16/3/2.
//  Copyright © 2016年 peijiaxing. All rights reserved.
//

import UIKit
import SnapKit

class JXRetweetView: UIView {

    
    var bottomConstaint: Constraint?
    
    var retweetViewModel : JXStatusViewModel?{
        
        didSet{
            contentLabel.text = retweetViewModel?.retweetStatusString
            
            self.bottomConstaint?.uninstall()
            
            // 判断是否有图片  并设置约束
            if let pic_url = retweetViewModel?.statusModel?.retweeted_status?.pic_urls where pic_url.count > 0 {
                pictureView.hidden = false
               
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                    
                })
                
                // 传值
                var result = [JXPictureViewModel]()
                
                for model in pic_url{
                    let viewModel = JXPictureViewModel()
                    
                    viewModel.model = model
                    
                    result.append(viewModel)
                }
                pictureView.pictureList = result
            }else{
                
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right).offset(-CellMargin)
//            make.bottom.equalTo(self.snp_bottom)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
            
            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
}
    
    
    
    
    private lazy var contentLabel :UILabel = {
       
        let label = UILabel()
        label.text = "转发微博"
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.redColor()
        label.numberOfLines = 0
        return label
        
    }()
    
    private lazy var pictureView : JXPictureView  = JXPictureView()
}
